
import java.io.IOException;
import java.io.InputStream;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.m2g.SVGAnimator;
import javax.microedition.m2g.SVGEventListener;
import javax.microedition.m2g.SVGImage;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGRGBColor;
import org.w3c.dom.svg.SVGSVGElement;


public class SVG {
	public String IMAGE = "/svgImage.svg";
	protected Canvas canvas;
	private SVGImage svgImage;
	protected SVGAnimator svgAnimator;
//	public String IMAGE;
//	private Display display;
	private Command cmExit;
	private Command cmOpenFile;
	private Command cmZoom;
	private Command cmFindMe;

	public SVG(){
		try {
			InputStream is = getClass().getResourceAsStream(IMAGE);
//			display = Display.getDisplay(this);
			svgImage = (SVGImage)SVGImage.createImage(is, null);
			svgAnimator = SVGAnimator.createAnimator(svgImage);
			svgAnimator.setSVGEventListener(new MySvgEventListener());

//			canvas = new SVGImageCanvas(svgImage);

			canvas = (Canvas)(svgAnimator.getTargetComponent());
//			display.setCurrent(canvas);
		} catch (IOException ex) {}
	}

	public class MySvgEventListener implements SVGEventListener{

		public void hideNotify() {
			// TODO Auto-generated method stub

		}

		public void keyPressed(int keyCode) {
			// TODO Auto-generated method stub
			int gameAction = canvas.getGameAction( keyCode );
			switch ( gameAction )
			{
			default:
				editSVG();
				// leave unchanged
			}
		}

		public void keyReleased(int arg0) {
			// TODO Auto-generated method stub

		}

		public void pointerPressed(int arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		public void pointerReleased(int arg0, int arg1) {
			// TODO Auto-generated method stub

		}

		public void showNotify() {
			// TODO Auto-generated method stub

		}

		public void sizeChanged(int arg0, int arg1) {
			// TODO Auto-generated method stub

		}
		public void editSVG(){
			System.out.println("Entrei no editSVG()...");
			Document document = svgImage.getDocument();
			SVGSVGElement root = (SVGSVGElement)document.getDocumentElement();

			Element raiz = document.getDocumentElement();
			SVGElement circle = (SVGElement)document.createElementNS("http://www.w3.org/2000/svg", "circle");

			SVGRGBColor fgColor = root.createSVGRGBColor(0, 255, 0);
			circle.setRGBColorTrait("fill", fgColor);
			circle.setFloatTrait("cx", (float)-43.2);//(float)LocationUI.coord.getLatitude());
			circle.setFloatTrait("cy", (float)-22.9);//(float)LocationUI.coord.getLongitude());
			circle.setFloatTrait("r", (float).5);

			root.appendChild(circle);
			canvas.repaint();

		}
	}
}

